
//加载自己的样式
require('./user-detail.less')

//服务器地址
const API = 'http://fitness.h5.itsource.cn:3701'

//引入axios工具函数
const request = require('../../../utils/request')

window.onload = () => {
  //获取dom
  const upload = document.querySelector('#upload')

  //获取用户信息
  function getUserInfo() {
    request.get('/api/user/info').then(res => {
      //1 解构数据
      let {errno, data} = res.data
      //2 判断
      if (errno === 0) {
        //3 处理业务逻辑
        render(data)
      }
    })
  }

  //渲染数据
  function render(data) {
    //图片渲染 服务器地址+相对路径+文件名
    avatar.src = API + data.imgUrl
  }

  //初始化获取用户信息
  getUserInfo()

  //监听file 输入框的change事件
  upload.addEventListener('change', e => {
    console.log('e :>> ', e.target.files[0])
    //创建FormData 对象 添加文件
    const formData = new FormData()
    formData.append('file', e.target.files[0])
    //发送ajax请求
    request.post('/api/shared/uploadPortrait', formData).then(res => {
      console.log('res.data :>> ', res.data)
    })
  })

  //获取dom
  const showPicker = document.querySelector('#showPicker')
  showPicker.addEventListener('click', () => {
    weui.picker(
      [
        {
          label: '男',
          value: 0,
        },
        {
          label: '女',
          value: 1,
        },
      ],
      {
        onChange: function (result) {
          console.log(result)
        },
        onConfirm: function (result) {
          console.log(result)
        },
        title: '性别选择',
      }
    )
  })

  //获取dom
  const showDatePicker = document.querySelector('#showDatePicker')
  //监听下拉点击
  showDatePicker.addEventListener('click', () => {
    console.log('666 :>> ', 666)
    weui.datePicker({
      start: 1990,
      end: new Date().getFullYear(),
      onChange: function (result) {
        console.log(result)
      },
      onConfirm: function (result) {
        console.log(result)
      },
      title: '多列选择器',
    })
  })
}
